
var isjump = 0;//判断是否跳转
var op_factory; //(参数名字不要取一样！)
var username;
var secretkey;

layui.config({
	version:'1510164124181'
}).use(['form', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	username = $.session.get("username");
	secretkey = $.session.get("user_secretkey");
    find_staff();
	find_staff_type();
	/*if(window.sessionStorage.getItem("op_factory")){
 		var data = JSON.parse(window.sessionStorage.getItem("op_factory"));
		$("#operation_type").val(data.operation_type);	//(类似于session)
		$("#starttime").val(data.starttime);
		pageNo = data.pageNo;
 	}*/
	operation_recard(pageRecords,pageNo);
});


//员工帐号
function find_staff(){
	$.ajax({
	url: "../../../../service",
	cache: false,
	async:false,
	data : {
	proc : "g_xmy.find_staff_account",
	para : "['" + username + "','" + secretkey + "']"
	},
	dataType : "json",
	success : function(data) {
	//console.log("员工=="+JSON.stringify(data));
		var uls = document.getElementById("staff_number");
		var i = '<option selected="selected" value="-1">请选择员工帐号</option>';
		for (var a = 0; a <= data.length - 1; a++) {
			i = i + "<option value='" +data[a].NAME+ "'>" + data[a].NAME + "</option>"
		}
		uls.innerHTML = i;

	}

	});
}

//员工岗位
function find_staff_type(){
	$.ajax({
	url: "../../../../service",
	cache: false,
	async:false,
	data : {
	proc : "g_xmy.find_staff_account_type",
	para : "['" + username + "','" + secretkey + "']"
	},
	dataType : "json",
	success : function(data) {
	//console.log("员工岗位=="+JSON.stringify(data));
		var uls2 = document.getElementById("operation_type");
		var i2 = '<option selected="selected" value="">请选择</option>';
		for (var aa = 0; aa <= data.length - 1; aa++) {
			i2 = i2 + "<option value='" +data[aa].NAME+ "'>" + data[aa].NAME + "</option>"
		}
		uls2.innerHTML = i2;

	}

	});
}

//搜索查询用户
function search(){
	isjump=0;
	operation_recard(pageRecords,pageNo);
}

function operation_recard(pageRecords,pageNo){
	var staff_number = document.getElementById("staff_number").value;
	var operation_type = document.getElementById("operation_type").value;
	/*if(operation_type==''){

	}*/
	var starttime = document.getElementById("starttime").value;
	var outtime = document.getElementById("outtime").value;
/*	op_factory='{"driver_number":"'+driver_number+'",';
	op_factory+='"operation_type":"'+operation_type+'",';
	op_factory+='"pageNo":"'+pageNo+'"}';
	window.sessionStorage.setItem("op_driver",op_driver);
    console.log("op_factory==="+op_factory);
  */
	if(starttime==""&&outtime!=""){
		layer.alert("请选择开始时间");
		return false;
	}
	if(starttime!=""&&outtime==""){
		layer.alert("请选择结束时间");
		return false;
	}
	var d1 = new Date(starttime);
	var d2 = new Date(outtime);
	if(d1>d2){
		layer.alert("结束时间不能小于开始时间");
		return false;
	}
  //console.log("参数===="+username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "','" + staff_number + "','" + operation_type + "','" + starttime  + "','" + outtime);

	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "g_xmy.operation_recard_staff",
			para:"['" + username + "','" + secretkey + "','" + pageRecords + "','" + pageNo + "','" + staff_number + "','" + operation_type + "','" + starttime  + "','" + outtime +"']"
		},
		dataType: "json",
		success: function(data){
		//	console.log("工厂操作记录===="+JSON.stringify(data));
			var tab=document.getElementById("tableid1");
			var ro=tab.rows.length;
			for(var b=1;;){
				if(tab.rows.length==1){
					break;
				}
				tab.deleteRow(b);
			}
			if (data[0].TAG == "0" ){
				layer.alert(data[0].MSG);
				top.location.href="../../login.html"
			}else{

				for(var a=0;a<data.length;a++){
					if(a==data.length-1){
						totalRecords = data[a].ID;
						totalPage = data[a].OPERATORID;
						if(isjump ==0){
							page();
							isjump = 1;
						}
						return;
					}
					var tables=document.getElementById("tableid1");
					var ro=tables.insertRow(a+1);
					cell1=ro.insertCell(0);
					cell1.innerHTML=data[a].ID;
					cell1.align="center";
					cell2=ro.insertCell(1);
					cell2.innerHTML=data[a].OPERATORID;
					cell2.align="center";
					cell3=ro.insertCell(2);
					cell3.innerHTML=data[a].ORDERNUM;
					cell3.align="center";
					cell4=ro.insertCell(3);
					cell4.innerHTML=data[a].USERNAME;
					cell4.align="center";
					cell5=ro.insertCell(4);
					cell5.innerHTML=data[a].USERPHONE;
					cell5.align="center";
					cell6=ro.insertCell(5);
					cell6.innerHTML=data[a].CLOTHCODE;
					cell6.align="center";
					cell7=ro.insertCell(6);
					cell7.innerHTML=data[a].OPERA_TIME;
					cell7.align="center";
					cell8=ro.insertCell(7);
					cell8.innerHTML=data[a].OPERATORWORK;
					cell8.align="center";
					cell9=ro.insertCell(8);
					cell9.innerHTML='<a href="#" onclick="delete_classification('+data[a].ID+')">删除</a>';
					cell9.align="center";
				}
			}
		}
	});
}

//分页
function page(){
	laypage.render({
		 elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
		,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,jump: function(obj, first){
			//首次不执行
			if(!first){
		//	  console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
		//	  console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
			  operation_recard(obj.limit,obj.curr);
			}
		  }
	});
}


//删除
function delete_classification(id){
	var username = $.session.get("username");
	var secretkey = $.session.get("user_secretkey");
	layer.confirm('确定删除该员工操作记录吗？', {
		  title:'系统提示',
		  btn: ['确定','取消']
		}, function(){
			 $.ajax({
				url: "../../../../service",
				data : {
					proc : "g_xmy.delete_staff_recard",
					para : "['" + username + "','" + secretkey + "','" + id + "']"
				},
				dataType : "json",
				success : function(data) {
					if (data[0].TAG == "1") {
						layer.alert("成功删除该员工操作记录!",function(index){
							 window.location.href="./operation_recard2.html";
							 layer.close(index);
						});
					}else{
						layer.alert("删除失败!");
					//	window.location.href="./error.html";
					}
				}
			});
		});

}
